package module::modewalk_us::parser;

use Encode;
use HTTP::Date;
#use strict;

# ---------------------------------------------
# constructor
# ---------------------------------------------
sub new {
	my $class = shift;
	my $self  = {};
	bless $self, $class;
	return $self;
}

# ---------------------------------------------
# parse one product detail page
# ---------------------------------------------
sub parseAndgetInfo {
	my $this = shift;
	my $url  = shift;
	my $charset ='UTF-8'; 
	my $data = $main::filedata;
	my $content;

	$content = $data->getPage( $url, $charset );
	if ( !$content ) {
		# E300001 : Failed to get detail information of XXX.
		Log::Writer::setLog( "E300001", { 'URL' => $url } );
		return;
	}
	#/store/product/tetris-bustier-dress
	#get productId
	my $productId;
	my %idHash;

	if($url =~ /\/product\/(.*)/is){
		$productId = $1;
		$productId =~ s/\///isg;
	}
	if ( !$productId ) {
		Log::Write::setLog( "E300002", { 'URL' => $url, 'POINT' => 'productId' } );
		return;
	}
	
	#GET description ####Modify BEGIN Louise 1/9/2013 #8857 ####
	my $description;
	my $key1 = decode('utf-8','ê');
	my $key2 = decode('utf-8','é');
	my $key3 = decode('utf-8','è');
	if($content =~ /<div class="details_block description">(.*?)<\/div>/is){
		$description = $1;
		$description =~ s/&nbsp;/ /isg;
		$description =~ s/&ecirc;/$key1/isg;
		$description =~ s/&eacute;/$key2/isg;
		$description =~ s/&egrave;/$key3/isg;
		$description =~ s/&rsquo;/'/isg;
		$description =~ s/<p>|<\/p>/<br>/isg;
		$description =~ s/^\s+|\s+$//isg;
	}
	####Modify END Louise 1/9/2013 #8857 ####
	
	if ( !$description ) {
		Log::Write::setLog( "E300002", { 'URL' => $url, 'POINT' => 'description' } );
	}
	
	#get imageUrl
	my $imageURL;
	if($content =~ /<div id="images" class="images">\s*<img src="([^"]*?)"/is){
		$imageURL = $1;
	}
	if($imageURL !~ /http:\/\//is){
		$imageURL = $data->trimURL($imageURL,$url); 
	}
	if ( !$imageURL ) {
		Log::Write::setLog( "E300002", { 'URL' => $url, 'POINT' => 'imageURL' } );
		return;
	}else{
		$imageURL =~ s/https/http/is;
	}
	#get sizes ####Modify BEGIN Louise 1/9/2013 #8857 ####
	my @sizes;
	if($content =~ /<div class="size_options[^"]*"[^>]*>(.*?)<div class="tooltip[^"]*">/is){
		my $sizeTemp = $1;
		while($sizeTemp =~ /<div id[^>]*class="option enabled">\s*<label>(.*?)<\/label>/isg){
			my $size =  $1;
			$size =~ s/<.*//isg;
			$size =~ s/^\s*|\s*$//isg;		
			push @sizes,$size;
		}
	}
	####Modify END Louise 1/9/2013 #8857 ####
	
	#get price
	my $price;
	my $salePrice;
	if($content =~ /<div class="price">\s*(.*?)\s*<\/div>/is){
		my $priceTemp = $1;
		if($priceTemp =~ /<span class="original">\s*(.*?)\s*<\/span>/is){
			$price = $1;
			$price =~ s/.*?\$/\$/isg;
			$price =~ s/\(|\)//isg;
		}
		if($priceTemp =~ /<span class="new">\s*(.*?)\s*<\/span>/is){
			$salePrice = $1;
			$salePrice =~ s/.*?\$/\$/isg;
			$salePrice =~ s/\(|\)//isg;
		}
		if($priceTemp =~ /<span class="retail">(.*?)<\/span>/is){
			$price = $1;
		}
	}
	if(!$price && $salePrice){
		$price = $salePrice;
		$salePrice ='';
	}
	if($price eq $salePrice){
		$salePrice ='';
	}
	if ( !$price ) {
		Log::Write::setLog( "E300002", { 'URL' => $url, 'POINT' => 'price' } );
		return;
	}
	my %product;
	# bulid Product
	%product = (
		'ProductId' => $productId,
		'Size'        => \@sizes,
		'SalePrice'   => $salePrice,
		'Price'       => $price,
		'Description' => $description,
		'ImageURL'    => $imageURL,
		'Time'        => Date::Time::getGMT()
	);
	return %product;
}

# ----------------------------------------------------
# judge the StringArray whether have the StringElement or not
# if have return false else return true
# Modify Log
# 01/20/2011 Peter create
# ----------------------------------------------------
#sub isHaveStringElement {
#	my $array   = shift;
#	my $element = shift;
#	my @arrays  = @{$array};
#	for my $arrayElement (@arrays) {
#		if ( $arrayElement eq $element ) {
#			return 0;
#		}
#	}
#	return 1;
#}



1;
